import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy, OnDestroy, ViewChild} from '@angular/core';
import {ImageServeService} from "app/shared/service/image/image-serve.service";

import {Subject} from "rxjs/Subject";
import { ToolsService } from "app/shared/service/tools/tools.service";
import {SeedInfiniteScroll} from "app/shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {GoodsService} from "app/pages/goods/goods.service";
import {Router} from "@angular/router";

@Component({
  selector: 'service-card-list',
  templateUrl: './service-card-list.component.html',
  styleUrls: ['./service-card-list.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ServiceCardListComponent implements OnInit , OnDestroy {
  public paramData = {
    busType: '',
    state: '',
    page: 1 ,
    rows: 10
  };

  public statusList ;
  public typeList  ;
  public cardList = [];
  public cardTotal = 0;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  constructor(
    private imageServe: ImageServeService,
    private goodsService: GoodsService ,
    private toolsservice: ToolsService,
    private router: Router,
  ) { }


  getImage(name: string): string {
    return this.imageServe.getImage(name);
  }
  getSelectResult(e) {
    console.log(e)
    this.paramData.state = e.status.value;
    this.paramData.busType = e.type.value;
    this.paramData.page = 1 ;
    this.cardList = [] ;
    this.getServiceCardList();
  }
  /**
   * @description 服务卡状态列表
   * @author chensk740
   * @date 2018-03-12
   */
  getListStatus() {
 this.goodsService.getListStatus()
   .takeUntil(this.ngUnsubscribe)
   .subscribe( res => {
     if (res.result === '0') {
       this.statusList = res.data ;
     }
    });
  }
  /**
   * @description 服务卡类型列表
   * @author chensk740
   * @date 2018-03-12
   */
  getListBusType() {
    this.goodsService.getBusTypeList()
      .takeUntil(this.ngUnsubscribe)
      .subscribe(res => {
        if (res.result === '0') {
          this.typeList = res.data ;
        }
      });
  }
  /**
   * @description 服务卡列表
   * @author chensk740
   * @date 2018-03-12
   * @param data 数据参数
   */
  getServiceCardList() {
    this.toolsservice.showLoading();
    this.goodsService.getServiceCardList(this.paramData)
      .takeUntil(this.ngUnsubscribe)
      .subscribe( res => {
        if (res.result === '0') {
          this.paramData.page++;
          this.infiniteScroll.complete();
          if (!res.data.hasNextPage ) {
            this.infiniteScroll.enable(false);
          }
          this.cardList = [...this.cardList , ...res.data.list];
          this.cardTotal = res.data.total;
        }
        this.toolsservice.hideLoading();
      });
  }

  // 跳转服务卡详情
  goToServiceCardDetail (card) {
    this.router.navigateByUrl(card.serviceCardStepUrl);
  }

  ngOnInit() {
    this.getListBusType();
    this.getListStatus();
    this.getServiceCardList();
  }
  ngOnDestroy() {
    // this.ngUnsubscrible.unsubscribe();
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
